﻿#include "Buffer.h"
#include "PUX264Encoder.h"
#include "x264encoder.h"
#include "Macro.h"


//写文件
int api_WriteFile(char * filename, char * mode, char * pbuf, int lenth)
{
	FILE *fp;
	int ret;
	fp = fopen(filename, mode);
	if (fp == NULL)
	{
		perror("fopen");
		return -1;
	}
	if (fwrite(pbuf, lenth, sizeof(char), fp) == -1)
	{
		perror("fwrite");
		return -1;
	}
	//printf("fwrite is count %d %s\r\n",lenth,filename); 
	fclose(fp);
}

int g_i = 0;

void callback(void* pdata, int datalen)
{
	printf("%p %d %d \n",pdata,datalen, g_i);
	api_WriteFile("/data/local/tmp/test.h264","a+",(char*)pdata,datalen);
}


int main()
{
	printf("main star h264!!!!\n"); 


#ifdef __ANDROID__
	printf("__ANDROID__ !!!!!!!!!!!!!!!\n");

#else
	printf("__ANDROID__ NO NO NO NO NO\n");
#endif



	getchar();
	FILE *in_file = fopen("test.yuv", "rb");	//视频YUV源文件 
	int in_w = 512, in_h = 768;//宽高	
	int framenum = 100;
	const char* out_file = "all.h264";					//输出文件路径
	int y_size = in_w  * in_h;
	uint8_t* picture_buf;

	picture_buf = (uint8_t *)malloc(y_size * 3 / 2 );

	initX264Encode(512, 768, 20, 0, callback);
	for (int i = 0; i<framenum; i++) {
		g_i++;

		printf("encoderH264 %d \n", g_i);
		//读入YUV
		if (fread(picture_buf, 1, y_size * 3 / 2, in_file) < 0)
		{
			printf("文件读取错误\n");
			return -1;
		}
		encoderH264(picture_buf, y_size * 3 / 2, 0);
	}

	releaseX264Encode();

	printf("main end h264!!!!\n");
	
	return 0;
}